package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealMapper setmealMapper;

    @ApiOperation("分页接口")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页", setmealPageQueryDTO);
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @CachePut(cacheNames="setmealCache",key="#setmealDTO.categoryId")
    @ApiOperation("新增套餐接口")
    @PostMapping
    public Result<String> save(@RequestBody SetmealDTO setmealDTO){
        log.info("套餐信息为{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("批量删除接口")
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除传入参数{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();

    }
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("启用停用接口功能")
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable("status") Integer status,Long id){
        setmealService.updateStatus(status,id);
        return Result.success();
    }
@GetMapping("/{id}")
@ApiOperation("根据id查关联套餐")
public Result<SetmealVO> get(@PathVariable Long id){
        log.info("套餐id为{}",id);
        SetmealVO setmealVO=setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
}
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @ApiOperation("修改套餐代码")
    @PutMapping()
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
        log.info("setmealDTO数据为{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

}
